package com.example.yigoumall.controller;

import com.example.yigoumall.entity.Order;
import com.example.yigoumall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @GetMapping
    public List<Order> getAllOrders() {
        return orderService.findAll();
    }
    
    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable Integer id) {
        return orderService.findById(id);
    }
    
    @GetMapping("/user/{userId}")
    public List<Order> getOrdersByUserId(@PathVariable Integer userId) {
        return orderService.findByUserId(userId);
    }
    
    @PostMapping
    public Order createOrder(@RequestBody Order order) {
        return orderService.save(order);
    }
    
    @PutMapping("/{id}")
    public Order updateOrder(@PathVariable Integer id, @RequestBody Order order) {
        order.setId(id);
        return orderService.save(order);
    }
    
    @DeleteMapping("/{id}")
    public void deleteOrder(@PathVariable Integer id) {
        orderService.deleteById(id);
    }
}